axios.defaults.baseURL = 'http://api-breakingnews-web.itheima.net'
axios.interceptors.request.use(function (config) {
    console.log(config);
    if (config.url.includes('/my/')) {
        config.headers.Authorization = localStorage.getItem('token')

    }

    return config
}, function (error) {
    return Promise.reject(error)

})
axios.interceptors.response.use(function (response) {
    let obj = response.data
    if (obj.message === '身份认证失败!') {
        location.href = '/login.html'
        localStorage.removeItem('token')
    }
    return response
}, function (error) {
    return Promise.reject(error)

})
function dateFormat(str) {
    let dt = new Date(str)
    let yy = dt.getFullYear()
    let mm = padZero(dt.getMonth() + 1)
    let dd = padZero(dt.getDate())
    let h = padZero(dt.getHours())
    let m = padZero(dt.getMinutes())
    let s = padZero(dt.getSeconds())
    return `${yy}-${mm}-${dd} ${h}:${m}:${s}`
}
function padZero(n) {
    return n < 10 ? '0' + n : n
}